<?php

// include menu
include "menu.phtml";
$question_name = ucfirst($this->question_attr['title']->getAttributeEntity()->getValue());

// ends with function
function endsWith($haystack, $needle) {
    return substr($haystack, -strlen($needle)) == $needle;
}

// back to list smart button
$url = $this->url(array('controller'=>'help'), 'default', true);
if(isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'help') !== false) {
	$exploded = explode('/page/', $_SERVER['HTTP_REFERER']);
	if(endsWith($exploded[0], '/help/index')
		|| endsWith($exploded[0], '/help/myquestions')
		|| endsWith($exploded[0], '/help/archives'))
			$url = $_SERVER['HTTP_REFERER'];
}

// imported entries
$imported = null;
$imported_time = strtotime('2013-02-01 15:00');
if(strtotime($this->question->getDateCreated()) < $imported_time)
	$imported = 'imported';

$this->headMeta()->appendProperty('og:title', $question_name);
$this->headMeta()->appendProperty('og:description', Petolio_Service_Util::shortenText(reset($this->attrs['description']), 200, true));
?>
<div id="right">
	<div class="rightbox">
		<?php if($this->flagged): ?>
			<div class="context"><div class="in"><a href="javascript:void(0);" id="context_menu" class="icon_link the_down"><span></span></a></div></div>
			<h1><?php echo $this->translate->_("Question:"); ?> <?php echo $question_name; ?></h1>
			<br />
			<div class="c_error">
				<div><b><?php echo $this->translate->_("Cannot display this question because it was flagged by the community."); ?></b></div>
				<ul>
					<?php foreach(array_unique($this->flagged) as $item): ?>
						<li><?php echo $item; ?></li>
					<?php endforeach; ?>
				</ul>
			</div>
		<?php else: ?>
			<div class="context"><div class="in">
				<a href="javascript:void(0);" id="context_menu" class="icon_link the_down"><span></span></a>
				<?php if ($this->hasIdentity && !$this->admin): ?>
					<a href="javascript:void(0);" class="icon_link caution openflag"><span><?php echo $this->translate->_("Flag as inappropriate"); ?></span></a>
					<div class="dialog-flag" title="<?php echo $this->translate->_("Flag as inappropriate"); ?>">
						<div id="in-flag" style="margin: 8px 0px 2px 0px; line-height: 18px;" rel="<?php echo $this->question->getId(); ?>_<?php echo $this->question->getUserId(); ?>">
							<span id="flag-text"><?php echo $this->translate->_("Please select the category that most closely reflects your concern about the content presented before you."); ?><br /><br /></span>
							<?php echo $this->flag; ?><br />
							<span class="grey"><small>
								<?php echo $this->translate->_("You may only flag as inappropriate once per content."); ?><br /><br />
								<?php echo $this->translate->_("Anything more than that would be against our Terms and Conditions and it may lead to the suspension or your account or a permanent ban."); ?><br /><br />
								<?php echo $this->translate->_("Abusing this feature is also a violation of our Terms and Conditions, so please don't do it."); ?>
							</small></span>
						</div>
					</div>
					<?php $flag_opt = 'PoHelp'; include "../application/modules/frontend/views/scripts/flag.phtml"; ?>
				<?php endif; ?>
			</div></div>
			<h1><?php echo $question_name; ?></h1>
			<br />
			<div class="left"><input onclick="Petolio.go('<?php echo $url; ?>');" type="button" value="<?php echo $this->translate->_("< Back to List"); ?>" id="submit" name="prev" style="margin: 0px;"><div class="clear"></div></div>
			<div class="clear"></div>
			<br />
			<div class="left" style="width: 430px; font-size: 12px; line-height: 18px;">
	 			<h2><?php echo $this->translate->_("Description"); ?></h2>
			    <div class="description_box <?php echo $imported; ?>"><?php echo reset($this->attrs['description']); ?></div>
			</div>
			<div class="right" style="width: 360px;">
			    <h2><?php echo $this->translate->_("Details"); ?></h2>
			    <table cellspacing="0" cellpadding="5" border="0" class="list">
					<?php foreach($this->attrs['details'] as $label => $value): ?>
		        		<tr>
		        			<th><?php echo $label; ?></th>
		        			<td><?php echo $value; ?></td>
		        		</tr>
					<?php endforeach; ?>
		    		<tr>
		    			<th style="width: 80px;"><?php echo $this->translate->_("Addressed To"); ?></th>
		    			<td><?php switch($this->question->getRights()) {
		    				case '0': echo $this->translate->_('All'); break;
		    				case '1': echo $this->translate->_('Friends'); break;
							case '2': echo $this->translate->_('Service Providers'); break;
		    			} ?></td>
		    		</tr>
		    		<tr>
		    			<th style="width: 80px;"><?php echo $this->translate->_("Owner"); ?></th>
		    			<td><a href="<?php echo $this->url(array('controller'=>'accounts', 'action'=>'view', 'user'=>$this->question->getUserId()), 'default', true); ?>"><?php echo $this->question->getOwner()->getName(); ?></a></td>
		    		</tr>
		    		<?php if($this->question->getPetId()): ?>
			    		<tr>
			    			<th style="width: 80px;"><?php echo $this->translate->_("Linked Pet"); ?></th>
			    			<td><a href="<?php echo $this->url(array('controller'=>'pets', 'action'=>'view', 'pet'=>$this->question->getPetId()), 'default', true); ?>"><?php $pet = $this->question->getPet(); echo $pet['name']; ?></a></td>
			    		</tr>
			    	<?php endif; ?>
					<?php if($this->question->getPetMedicalId()): ?>
			    		<tr>
			    			<th style="width: 80px;"><?php echo $this->translate->_("Linked Medical File"); ?></th>
			    			<td><a class="medical" rel="<?php echo $this->url(array('controller'=>'medical', 'action'=>'view', 'id'=>$this->question->getPetMedicalId()), 'default', true); ?>" href="javascript:void(0);"><?php $medical = $this->question->getMedical(); echo $medical->getHeadline1(); ?></a></td>
			    		</tr>
			    	<?php endif; ?>
			    </table>
				<?php if($this->admin): ?>
			    	<div class="tenpx"></div>
					<a href="<?php echo $this->url(array('controller'=>'help', 'action'=>'edit', 'question'=>$this->question->getId()), 'default', true); ?>" class='icon_link edit right'><span><?php echo $this->translate->_("Edit Question Details"); ?></span></a>
					<div class="cls"></div>
				<?php else: ?>
					<br />
				<?php endif; ?>
				<div class="clear"></div>
			</div>
			<div class="clear"></div>
			<br />
			<?php
			$image = PO_BASE_URL."images/new/large/help.png";
			if(count($this->pictures) > 0): ?>
			    <div class="pictures">
					<?php
					$first = true; 
					foreach($this->pictures as $idx => $pic):
						if($first) {
							$first = false;
							$image = PO_BASE_URL."images/userfiles/help/".$this->question->getId()."/".$pic;
						}
					?>
						<div class="pic">
							<span class="img" rel="<?php echo $idx; ?>" style="background: #000 url('/images/userfiles/help/<?php echo $this->question->getId(); ?>/small_<?php echo $pic; ?>') center center no-repeat;"></span>
						</div>
					<?php endforeach; ?>
					<div class="clear"></div>
				</div>
			    <div class="clear"></div>
			    <?php
			    	// if there is more then 1 page
			    	if ($this->picture_paginator->count() > 1) {
			    		echo $this->paginationControl($this->picture_paginator, 'Elastic', 'pagination-controls.phtml', array('pos' => 'bot'));
			    		echo '<div class="clear"></div>';
			    	}
			    ?>
			<?php endif; 
			$this->headMeta()->appendProperty('og:image', $image); ?>

			<?php if(count($this->pdfs) > 0): ?>
				<div style="margin: 10px 0px; line-height: 16px;">
					<h2><?php echo $this->translate->_("Attached PDFs"); ?></h2>
		    		<div class="description_box">
						<?php foreach($this->pdfs as $idx => $pdf): ?>
							<a href="<?php echo $this->url(array('controller'=>'pets', 'action'=>'files', 'download' => $idx), 'default', true); ?>"><?php echo $pdf['name']; ?></a><span class="grey"> &nbsp;&bull;&nbsp; <?php echo $pdf['size']; ?></span><br />
						<?php endforeach; ?>
		    		</div>
		    	</div>
			<?php endif; ?>
			<?php if($this->admin): ?>
				<a href="<?php echo $this->url(array('controller'=>'help', 'action'=>'files', 'question'=>$this->question->getId()), 'default', true); ?>" class='icon_link edit right'><span><?php echo $this->translate->_("Edit Files"); ?></span></a>
				<div class="clear"></div>
			<?php endif; ?>
			
			<?php if(count($this->videos) > 0): ?>
				<hr />
			    <div class="pictures">
					<?php foreach($this->videos as $video): ?>
						<?php
							// get video entity
							$entity = $video->getMapper();

							// get video thumbnail
							$thumbs = $entity->getVideoThumbnails();
							$thumbnail = $thumbs[1]['url'];

							// get video duration
							$duration = date("i:s", $entity->getVideoDuration());
						?>
						<div class="pic">
							<span class="vid" rel="<?php echo $video->getId(); ?>" style="background: #000 url('<?php echo $thumbnail; ?>') center center no-repeat;"></span>
							<span class="duration"><?php echo $duration; ?></span>
						</div>
					<?php endforeach; ?>
					<div class="clear"></div>
				</div>
				<?php if($this->admin): ?>
					<div class="tenpx"></div>
					<a href="<?php echo $this->url(array('controller'=>'help', 'action'=>'videos', 'question'=>$this->question->getId()), 'default', true); ?>" class='icon_link edit right'><span><?php echo $this->translate->_("Edit Videos"); ?></span></a>
					<div class="clear"></div>
				<?php endif; ?>
			<?php endif; ?>
			<?php if(count($this->pictures) > 0 || count($this->videos) > 0): ?>
			<br />
			<?php endif; ?>
			<?php if($this->can): ?>
				<div class="left"><input onclick="Petolio.go('<?php echo $this->url(array('controller'=>'help', 'action'=>'answer', 'question'=>$this->question->getId()), 'default', true); ?>');" type="button" value="<?php echo $this->translate->_("Answer Question"); ?>" id="submit" name="prev" style="margin: 0px;"><div class="clear"></div></div>
			<?php else: ?>
				<div class="left"><input onclick="Petolio.go('<?php echo $url; ?>');" type="button" value="<?php echo $this->translate->_("< Back to List"); ?>" id="submit" name="prev" style="margin: 0px;"><div class="clear"></div></div>
			<?php endif; ?>
			<div class="clear"></div>
		<?php endif; ?>
	</div>
	<div class="rightbox" id="pet-social">
		<h1><?php echo count($this->answers); ?> <?php echo count($this->answers) == 1 ? $this->translate->_("Answer") : $this->translate->_("Answers"); ?></h1>
		<br />
		<?php if(isset($this->answers) && count($this->answers) > 0): ?>
			<div class="entries" style="width: 800px;">
				<?php foreach($this->answers as $idx => $answer):
					// imported entries
					$imported = null;
					if(strtotime($answer['date_created']) < $imported_time)
						$imported = 'imported';

					// avatar control
					$avatar = $this->partial('avatar.phtml', array(
						'user' => array('id' => $answer['user_id'], 'avatar' => $answer['user_avatar'])
					));
				?>
					<?php if($idx != 0): ?>
						<div class="delimiter"></div>
					<?php endif; ?>
					<div class="entry">
						<table>
						<col width="1%"><col>
						<tr>
							<td valign="top" style="vertical-align: top;">
								<div class="avatar">
									<img style="display: block; margin-top: 1px; border: 1px solid white;" src="<?php echo $avatar; ?>" />
								</div>
							</td>
							<td>
								<div class="data" style="width: 740px;">
									<div class="desc">
										<a style="font-weight: bold;" href="<?php echo $this->url(array('controller'=>'accounts', 'action'=>'view', 'user'=>$answer['user_id']), 'default', true); ?>"><?php echo $answer['user_name']; ?></a>
										<div class="<?php echo $imported; ?>">
											<?php echo $answer['answer']; ?>
										</div>
									</div>
									<?php if(count($answer['pictures']) > 0): ?>
										<div class="pictures">
											<?php foreach($answer['pictures'] as $idx => $pic): ?>
												<div class="pic">
													<span class="img" rel="<?php echo $idx; ?>" style="background: #000 url('/images/userfiles/help/<?php echo $this->question->getId(); ?>/small_<?php echo $pic; ?>') center center no-repeat;"></span>
												</div>
											<?php endforeach; ?>
											<div class="clear"></div>
										</div>
									    <div class="clear"></div>
									<?php endif; ?>
								    <?php if(count($answer['pdfs']) > 0): ?>
										<div style="margin: 10px 0px 3px 0px; padding: 5px; background-color: #F8F8F8; border: 1px solid #EEE;">
											<b><?php echo $this->translate->_("Attached PDFs"); ?></b>
											<div class="fivepx"></div>
											<?php foreach($answer['pdfs'] as $idx => $pdf): ?>
												<a href="<?php echo $this->url(array('controller'=>'pets', 'action'=>'files', 'download' => $idx), 'default', true); ?>"><?php echo $pdf['name']; ?></a><span class="grey smaller"> &nbsp;&bull;&nbsp; <?php echo $pdf['size']; ?></span><br />
											<?php endforeach; ?>
										</div>
									<?php endif; ?>
								</div>
								<div class="data links small">
									<div class="left">
										<span title="<?php echo Petolio_Service_Util::formatDate($answer['date_created'], Petolio_Service_Util::MEDIUMDATE, true, true); ?>"><?php echo Petolio_Service_Util::formatTime(strtotime($answer['date_created'])); ?></span>
										<?php if($answer['owner'] || $this->admin): ?>
											&nbsp;&bull;&nbsp;
											<a href="<?php echo $this->url(array('controller'=>'help', 'action'=>'edit', 'answer'=>$answer['id']), 'default', true); ?>"><?php echo $this->translate->_('Edit Answer'); ?></a> &nbsp;&bull;&nbsp;
											<a href="<?php echo $this->url(array('controller'=>'help', 'action'=>'files', 'answer'=>$answer['id']), 'default', true); ?>"><?php echo $this->translate->_('Edit Files'); ?></a> &nbsp;&bull;&nbsp;
											<a href="<?php echo $this->url(array('controller'=>'help', 'action'=>'delete', 'answer'=>$answer['id']), 'default', true); ?>" class="reqconf" title="<?php echo $this->translate->_('Are you sure you want to delete this answer?'); ?>"><?php echo $this->translate->_('Delete'); ?></a>
										<?php endif; ?>
									</div>
									<div class="clear"></div>
								</div>
							</td>
						</tr>
						</table>
					</div>
				<?php endforeach; ?>
			</div>
		<?php else: ?>
			<b class="red bigger"><?php echo $this->translate->_('No answers yet :('); ?></b>
		<?php endif; ?>
	</div>
	<div class="rightbox" style="padding: 0px; border: 0px;">
		<div class="left"><input onclick="Petolio.go('<?php echo $url; ?>');" type="button" value="<?php echo $this->translate->_("< Back to List"); ?>" id="submit" name="prev" style="margin: 0px;"><div class="clear"></div></div>
		<?php if($this->can): ?>
			<div class="left"><input onclick="Petolio.go('<?php echo $this->url(array('controller'=>'help', 'action'=>'answer', 'question'=>$this->question->getId()), 'default', true); ?>');" type="button" value="<?php echo $this->translate->_("Answer Question"); ?>" id="submit" name="prev" style="margin: 0px 0px 0px 5px;"><div class="clear"></div></div>
		<?php endif; ?>
		<div class="clear tenpx"></div>
		
		<div class="right">
			<?php echo $this->partial('addthis.phtml', array('title' => $question_name, 'translate' => $this->translate)); ?>
		</div>
		
	</div>
</div>
<?php $imgsw_path = array(
	'image' => "/images/userfiles/help/{$this->question->getId()}/{image}",
	'audio' => "/images/userfiles/help/{$this->question->getId()}/audios/{audio}",
	'video' => "http://www.youtube.com/embed/{video}"
); include "../application/modules/frontend/views/scripts/imgsw.phtml"; ?>
<script type="text/javascript">
	var Medical = function() {
		var identity = 'dialog-medical';
		
		function open(url) {
			$("#"+ identity).remove();
			$('<div id="'+ identity +'" title="<?php echo $this->translate->_("View Medical File"); ?>" class="ui-state-highlight">'+
				'<div style="position: relative; margin: 8px 0px 2px 0px;">'+
					'<div id="frame_overlay" style="opacity: 0.9; background: white url(/images/ajax-loader.gif) center center no-repeat; position: absolute; top: 1px; left: 1px; width: 837px; height: 500px;"></div>'+
					'<iframe id="frame" name="medical" src="'+ url +'/iframe/true" width="837" height="500" scrolling="no" style="display: block; border: 1px solid #ddd;"></iframe>'+
				'</div>'+
			'</div>').dialog({
				resizable: false,
				modal: true,
				width: 862,
				minHeight: 50,
				buttons: [{
					text: '<?php echo $this->translate->_("Close"); ?>',
					click: function() {
						$(this).dialog("close");
					}
				}],
				close: function() {
					$("#"+ identity).remove();
				},
				open: function() {
					$('#frame').load(function() {
						$('#frame_overlay').fadeOut('fast');
						$("#frame").contents().find('a').click(function() {
							if( !$(this).hasClass('showloading') )
								return true;

							$('#frame_overlay').fadeIn('fast');
						});
					});
				}
			});
		};

		function error(message) {
			$("#"+ identity).remove();
			$('<div id="'+ identity +'" title="<?php echo $this->translate->_("View Medical File"); ?>" class="ui-state-highlight">'+
				'<div style="position: relative; margin: 8px 0px 2px 0px;">'+
					'<p>' + message + '</p>' + 
				'</div>'+
			'</div>').dialog({
				resizable: false,
				modal: true,
				width: 400,
				minHeight: 50,
				buttons: [{
					text: '<?php echo $this->translate->_("Close"); ?>',
					click: function() {
						$(this).dialog("close");
					}
				}],
				close: function() {
					$("#"+ identity).remove();
				}
			});
		};

		function __construct() {
			$('a.medical').click(function() {
				<?php if($this->auth->hasIdentity()): ?>
					open($(this).attr('rel'));
				<?php else: ?>
					error('<?php echo $this->translate->_("Please login to see the medical record"); ?>');
				<?php endif; ?>
			});
		};

		return {
			init: __construct
		}
	}();

	READY(Medical.init);
</script>